package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.qo.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItmeServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        ServiceItem ser = new ServiceItem();
        ser.setName(serviceItem.getName());
        ser.setOriginalPrice(serviceItem.getOriginalPrice());
        ser.setDiscountPrice(serviceItem.getDiscountPrice());
        ser.setCarPackage(serviceItem.getCarPackage());
        ser.setInfo(serviceItem.getInfo());
        ser.setCreateTime(new Date());
        ser.setServiceCatalog(serviceItem.getServiceCatalog());
        ser.setAuditStatus(0);
        serviceItemMapper.insert(ser);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem serviceItem1 = this.get(serviceItem.getId());
        if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())
                || ServiceItem.SALESTATUS_ON.equals(serviceItem1.getSaleStatus())) {
            throw new BusinessException("操作错误");
        }

        ServiceItem ser = new ServiceItem();
        ser.setId(serviceItem.getId());
        ser.setName(serviceItem.getName());
        ser.setOriginalPrice(serviceItem.getOriginalPrice());
        ser.setDiscountPrice(serviceItem.getDiscountPrice());
        ser.setInfo(serviceItem.getInfo());
        ser.setServiceCatalog(serviceItem.getServiceCatalog());

        if (serviceItem1.getCarPackage()) {
            ser.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else {
            ser.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }

        serviceItemMapper.updateByPrimaryKey(ser);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    //上架
    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = this.get(id);
        if (serviceItem.getCarPackage()
                && ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())) {
            serviceItemMapper.sale(ServiceItem.SALESTATUS_ON, serviceItem.getId());
        } else if (ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(serviceItem.getAuditStatus())) {
            serviceItemMapper.sale(ServiceItem.SALESTATUS_ON, serviceItem.getId());
        } else {
            throw new BusinessException("操作错误");
        }
    }

    @Override
    public void saleOff(Long id) {
        serviceItemMapper.sale(ServiceItem.SALESTATUS_OFF, id);
    }

    @Transactional
    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long director,Long finance,  String info) {
        //如果我点击发起审核的话该怎么操作?
        //需要判断是否是套餐和审核状态和审核通过
        ServiceItem serviceItem = this.get(id);
        if (ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage()) ||
                ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())||
                ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("操作错误");
        }

        //改状态
        serviceItemMapper.changeAuditStatus(id, ServiceItem.AUDITSTATUS_AUDITING);

        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);
        //1 审核列表增加一条数据
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setServiceItemId(id);
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setServiceItem(serviceItem);
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setInfo(info);
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAudit.setAuditorId(director);
        carPackageAudit.setBpmnInfoId(bpmnInfoId);
        carPackageAuditMapper.insert(carPackageAudit);



        //创建流程实例
        Map<String, Object> map = new HashMap<>();
        if(director != null){
            map.put("director", director.toString());
        }
        if(finance != null){
            map.put("finance", finance.toString());
        }
        map.put("discountPrice", serviceItem.getDiscountPrice().longValue());

        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(),
                        carPackageAudit.getId().toString(),map);


        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateById(carPackageAudit);

    }

}
